package cn.zebra.dev.admin.module.menu.service.impl;

import cn.zebra.dev.admin.module.menu.dao.AdminMenuDao;
import cn.zebra.dev.admin.module.menu.entity.AdminMenuEntity;
import cn.zebra.dev.admin.module.menu.service.AdminMenuService;
import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 后台菜单管理
 *
 * @author runnable
 * @date 2019-09-07 16:59:45
 */
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuDao, AdminMenuEntity> implements AdminMenuService {

    @Override
    public PageUtils<AdminMenuEntity> queryPage(Map<String, Object> params) {
        IPage<AdminMenuEntity> page = this.page(
                new Query<AdminMenuEntity>(params).getPage(),
                new QueryWrapper<>()
        );

        return new PageUtils<>(page);
    }

    @Override
    public List<AdminMenuEntity> findAdminUserMenuList(Long adminId) {
        List<AdminMenuEntity> allList = baseMapper.findAdminUserMenuList(adminId);
        List<AdminMenuEntity> result = new ArrayList<>();
        allList.stream().filter(menu -> menu.getParentId().equals(0L)).forEach(menu -> {
            menu.setChildren(getChildren(allList, menu.getId()));
            result.add(menu);
        });
        return result;
    }

    @Override
    public List<AdminMenuEntity> queryList() {
        return baseMapper.queryList();
    }

    @Override
    public Boolean saveMenu(AdminMenuEntity entity, Long createId) {
        entity.setDeleteFlag(false);
        entity.setCreateId(createId);
        entity.setCreateTime(LocalDateTime.now());
        return save(entity);
    }

    @Override
    public Boolean updateMenu(Long id, AdminMenuEntity entity, Long updateId) {
        entity.setDeleteFlag(false);
        entity.setUpdateId(updateId);
        entity.setUpdateTime(LocalDateTime.now());
        return update(entity, "id = {0}", id);
    }

    @Override
    public List<AdminMenuEntity> queryListParentId(Long id) {
        QueryWrapper<AdminMenuEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("parent_id = {0} AND delete_flag = {1}", id, false);
        wrapper.orderByAsc("sort");
        return list(wrapper);
    }

    @Override
    public Boolean deleteMenu(Long id) {
        AdminMenuEntity entity = new AdminMenuEntity();
        entity.setDeleteFlag(true);
        return update(entity, "id = {0}", id);
    }

    @Override
    public List<AdminMenuEntity> findNotButtonList() {
        QueryWrapper<AdminMenuEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("menu_type != 2 AND delete_flag = FALSE");
        wrapper.orderByAsc("sort");
        return list(wrapper);
    }

    @Override
    public List<AdminMenuEntity> getChildren(List<AdminMenuEntity> allList, Long parentId) {
        List<AdminMenuEntity> result = new ArrayList<>();
        allList.stream().filter(menu -> menu.getParentId().equals(parentId)).forEach(menu -> {
            menu.setChildren(getChildren(allList, menu.getId()));
            result.add(menu);
        });
        return result;
    }

    @Override
    public AdminMenuEntity findById(Long id) {
        return baseMapper.findById(id);
    }

}
